package com.meepo.feed.topic

import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.ethanhua.skeleton.Skeleton
import com.ethanhua.skeleton.SkeletonScreen
import com.meepo.basic.router.RouterManager
import com.meepo.basic.components.ImageLoaderFactory
import com.meepo.basic.components.eventbus.Event
import com.meepo.basic.components.listfragment.ListFragmentParams
import com.meepo.basic.components.view.MeepoFragment
import com.meepo.basic.schema.common.PageResult
import com.meepo.framework.design.helper.StatusBarHelper
import com.meepo.framework.design.recycler.LinearAdapter
import com.meepo.framework.design.recycler.ViewHolder
import com.meepo.feed.API
import com.meepo.feed.R
import com.meepo.basic.schema.feed.Topic
import com.meepo.basic.schema.user.User.Companion.SELF
import com.meepo.sdk.utils.ActivityHelper
import kotlinx.android.synthetic.main.topic_fragment.*
import kotlinx.android.synthetic.main.topic_fragment.refresh_layout
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class TopicFragment : MeepoFragment() {

    private var mSkeletonScreen: SkeletonScreen? = null

    private lateinit var mListAdapter: LinearAdapter<Topic>

    private lateinit var mTopicListFragment: TopicListFragment

    override fun refreshOnVisible(): Boolean = true

    @Subscribe(threadMode = ThreadMode.POSTING)
    fun onEvent(event: Event) {
        Log.e("TopicFragment", event.toString())
    }

    override fun enableEventBus(): Boolean = true
    private fun applyJoinedTopics(topics: PageResult<Topic>?) {

        mSkeletonScreen?.hide()

        topics?.items?.let {

            val list = ArrayList<Topic>(it)
            list.add(
                Topic(
                    id = "more",
                    title = "更多",
                    icon = "https://ec-resources.oss-cn-beijing.aliyuncs.com/%E6%9B%B4%E5%A4%9A.png"
                )
            )

            mListAdapter.refresh(list)
        }

        refresh_layout.finishRefresh()
    }

    override fun initView(view: View, savedInstanceState: Bundle?) {


        mListAdapter = object : LinearAdapter<Topic>(this.requireContext()) {
            override fun getItemLayoutId(item: Topic, position: Int): Int {
                return R.layout.topic_list_simple_item_layout
            }

            override fun bind(item: Topic, holder: ViewHolder, position: Int) {
                holder.setText(R.id.title_tv, item.title)
                holder.setImagePath(R.id.icon_iv, item.icon, ImageLoaderFactory.getImageLoader(requireContext(), 500))

                holder.itemView.setOnClickListener {
                    if (this.itemCount - 1 == position) {
//                            TopicSummaryListActivity.launch(requireActivity(), "我加入的")

                        ActivityHelper.launch(requireContext(), TopicListSimpleActivity::class.java)
                    } else {
                        RouterManager.FeedModule.routeToFeedTopicDetail(requireContext(), item.id)
                    }
                }
            }
        }

        joined_topic_recycler?.layoutManager = LinearLayoutManager(requireContext(), RecyclerView.HORIZONTAL, false)
        joined_topic_recycler?.adapter = mListAdapter

        // 骨架屏
        mSkeletonScreen = Skeleton.bind(joined_topic_recycler)
            .adapter(mListAdapter)
            .shimmer(true)
            .angle(20)
            .frozen(false)
            .duration(1200)
            .load(R.layout.topic_list_simple_item_skeleton_layout)
            .count(10)
            .show()

        StatusBarHelper.immerse(this.requireActivity(), refresh_layout)

        refresh_layout.setEnableLoadMore(false)
        refresh_layout.setReboundDuration(1000)
        refresh_layout.setOnRefreshListener {
            initData()
        }

        mTopicListFragment = TopicListFragment.instance(listFragmentParams = ListFragmentParams(enablePullRefresh = false))
//            RouterManager.FeedModule.routeToTopicList(listFragmentParams = ListFragmentParams(enablePullRefresh = false)) as TopicListFragment
        loadRootFragment(R.id.topic_list_frame, mTopicListFragment)
    }


    override fun setUpContentLayout(): Int = R.layout.topic_fragment
    override fun initData() {
        presenter.call(
            API.instance().fetchTopics(page = 1, size = 7, joined = SELF),
            { rsp -> applyJoinedTopics(rsp.data) },
            { applyJoinedTopics(null) }
        )

    }

    companion object {
        private var mFragment: TopicFragment? = null
        fun instance(): TopicFragment {
            if (mFragment == null) {
                mFragment = TopicFragment()
            }
            return mFragment!!
        }
    }
}